package cn.edu.zafu.bigdata.hdb;
import java.net.*;

public class TimeClient {
    public static void main(String[] args) {
        String serverAddress = "localhost"; // 服务端 IP 地址
        int port = 12345; // 服务端端口号

        try (DatagramSocket socket = new DatagramSocket()) {
            // 发送请求
            String request = "请求当前时间";
            byte[] requestData = request.getBytes();
            DatagramPacket requestPacket = new DatagramPacket(requestData, requestData.length,
                    InetAddress.getByName(serverAddress), port);
            socket.send(requestPacket);
            System.out.println("发送请求: " + request);

            // 接收服务端回复
            byte[] buffer = new byte[1024];
            DatagramPacket responsePacket = new DatagramPacket(buffer, buffer.length);
            socket.receive(responsePacket);

            // 处理回复
            String currentTime = new String(responsePacket.getData(), 0, responsePacket.getLength());
            System.out.println("从服务端接收到的当前时间: " + currentTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
